package com.bj.zspace.redis.study;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class ShuSeqiuUtils {

    public static String get(String path) throws Exception {
        // 创建 URL 对象
        URL url = new URL(path);
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为 GET
        connection.setRequestMethod("GET");

        // 设置 Cookie
        String cookie = "HMF_CI=b2cdbc67c0ee409f07b70b712045eb860a657d0d7e9e3547b5ea73c26cfac336d320c8d7e9a22c65ab274c9211a5b38b5407f4db1550fa285c2666d7d3830f3a9d; 21_vq=334";
        connection.setRequestProperty("Cookie", cookie);
        connection.setRequestProperty("sec-ch-ua-platform", "macOS");
        connection.setRequestProperty("sec-ch-ua-mobile", "?0");
        connection.setRequestProperty("sec-ch-ua", "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Google Chrome\";v=\"138\"");

        connection.setRequestProperty("Accept-Charset", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
        connection.setRequestProperty("Connection", "keep-alive");
        connection.setRequestProperty("Sec-Fetch-Dest", "document");
        connection.setRequestProperty("Sec-Fetch-Mode", "navigate");
        connection.setRequestProperty("Sec-Fetch-Site", "none");
        connection.setRequestProperty("Sec-Fetch-User", "?1");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36");
        connection.setRequestProperty("Sec-Fetch-User", "?1");

        // 获取响应码
        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);

        // 读取响应内容
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        // 断开连接
        connection.disconnect();
        return response.toString();
    }

    public static void println(List<String> redSet, String key) {
        System.out.println(key + "===========================================================================================");
        System.out.println(redSet.size());
        //redSet.sort(Comparator.comparingInt(o -> Integer.valueOf(o.split(",")[0])));
        redSet.forEach(System.out::println);
    }

    public static void sortPrintln(Map<String, Integer> redMap02, String flag) {
        System.out.println(flag + "===========================================================================================");
        List<Pair1> newReadO2Map = new ArrayList<>();
        redMap02.forEach((key, value) -> newReadO2Map.add(new Pair1(key, value)));
        newReadO2Map.sort(Comparator.comparingInt(o -> o.value));
        newReadO2Map.forEach(pair -> System.out.println(pair.key + ":" + pair.value));
    }

    public static void sortPrintln1(Map<Integer, Integer> redMap, String flag) {
        System.out.println(flag + "===========================================================================================");
        List<Pair> newReadO2Map = new ArrayList<>();
        redMap.forEach((key, value) -> newReadO2Map.add(new Pair(key, value)));
        newReadO2Map.sort(Comparator.comparingInt(o -> o.value));
        newReadO2Map.forEach(pair -> System.out.println(pair.key + ":" + pair.value));
    }

}
